#! /bin/sh

op=$1

os=`uname -s`
awkc=awk
if [ "$os" = "SunOS" ]; then
   awkc=nawk
fi

griduser=${LB_ORACLE_USER}
if [ -z "$griduser" ]; then
    griduser=`ps -ef | grep asm_pmon | grep -v grep | head -n1 | sed -e 's/^[ \t]*//' | cut -d" " -f1`
fi
if [ -z "$griduser" ];then
    griduser=grid
fi

user_id=`id ${griduser}`
if [ -z "$user_id" ];then
    echo "can't find user: ${griduser}"
    exit 1
fi

gridhome=${LB_ORACLE_GRID}
if [ -z "$gridhome" ]; then
    gridhome=`cat /etc/oratab | grep +ASM | grep -v "^#" | head -n1 | cut -d":" -f2`
fi
if [ -z "$gridhome" ];then
    gridhome=`ps -ef | grep evmlogger | grep -v grep | awk NR==1 | awk -F"/bin" '{print $(NF-1)}' | awk '{print $NF}'`
fi
if [ -z "$gridhome" ];then
    gridhome=`su - $griduser -c "echo \$ORACLE_HOME"`
fi

listener=${gridhome}/network/admin
TNS_ADMIN=${listener}

PATH=${gridhome}/bin:$PATH
ORACLE_HOME=${gridhome}
LD_LIBRARY_PATH=${gridhome}/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

export PATH
export ORACLE_HOME
export TNS_ADMIN
export LD_LIBRARY_PATH

echo_help()
{
    echo "Usage: $0 { create <group_name> <sid> <disks> }"
    echo "Usage: $0 { delete <group_name> <sid> <force> }"
    echo "Usage: $0 { rename <group_name> <sid> <disks> <newName> }"
    echo "Usage: $0 { connect <group_name> }"
    echo "Usage: $0 { disconnect <group_name> }"
}

validate_name()
{
    a=$1

    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_sid()
{
    a=$1

    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_disks()
{
    a=$1

    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_force()
{
    a=$1

    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

create()
{
    group_name=`echo $1 | tr '[a-z]' '[A-Z]'`
    sid=$2
    disks=$3

    checkDiskString

    disk_options=""
    flag=`echo "${disks}" | grep ","`
    if [ -z "$flag" ];then
        status=`echo "${disks}" | grep "/dev"`
        if [ -z "$status" ];then
            disks=`echo "/dev/r${disks}"`
        fi
        disk_options="-disk '${disks}'"
    else
        ids=`echo "${disks}" | tr ',' ' '`
        for disk in ${ids};
        do
            status=`echo "${disk}" | grep "/dev"`
            if [ -z "$status" ];then
                disk=`echo "/dev/r${disk}"`
            fi
            disk_options=`echo "${disk_options} -disk '${disk}'"`;
        done
    fi
    if [ -z "$disk_options" ];then
        echo "invalid diskgroup -disk"
        exit 1
    fi

    ASMCA=${gridhome}/bin/asmca
    su - ${griduser} -c "${ASMCA} -silent -createDiskGroup -diskGroupName ${group_name} ${disk_options} -redundancy external -au_size 8"

    ASMCMD=${gridhome}/bin/asmcmd
    status=`su - ${griduser} -c "${ASMCMD} ls" | grep "${group_name}"`
    if [ -z "$status" ];then
        su - ${griduser} -c "${ASMCMD} mount ${group_name}"
    fi

    status=`su - ${griduser} -c "${ASMCMD} ls" | grep "${group_name}"`
    if [ -z "$status" ];then
        echo "create completed, but could find diskgroup by asmcmd"
        exit 1
    fi

    su - ${griduser} -c "${ASMCMD} mkdir ${group_name}/archivelog"
    su - ${griduser} -c "${ASMCMD} mkdir ${group_name}/datafile"
    su - ${griduser} -c "${ASMCMD} mkdir ${group_name}/controlfile"

    echo "${group_name}"
}

rename()
{
    group_name=`echo $1 | tr '[a-z]' '[A-Z]'`
    sid=$2
    disks=$3
    newName=`echo $4 | tr '[a-z]' '[A-Z]'`

    checkDiskString

    ASMCMD=${gridhome}/bin/asmcmd
    status=`su - ${griduser} -c "${ASMCMD} ls" | grep ${group_name}`
    if [ -n "$status" ];then
        su - ${griduser} -c "${ASMCMD} umount ${group_name}"
    fi

    KFED=${gridhome}/bin/kfed
    tmp=tmp.txt
    ids=`echo "${disks}" | tr ',' ' '`
    for disk in ${ids};
    do
        status=`echo "${disk}" | grep "/dev"`
        if [ -z "$status" ];then
            disk=`echo "/dev/r${disk}"`
        fi
        if [ -z "$disk" ];then
            continue
        fi
        targetdg=`${KFED} read ${disk} | grep "grpname" | awk -F ':' '{print $2}' | awk -F';' '{print $1}' | sed 's/ //g'`
        if [ -f "${disk}" ];then
            continue
        fi

        if [ "$targetdg" != "$group_name"  ];then
            continue
        fi

        ${KFED} read ${disk} | grep "grpname" | sed "s/${group_name}/${newName}/g" > ${tmp}
        ${KFED} merge ${disk} text=${tmp}
    done
    rm -rf ${tmp}

    echo "${newName}"
}

connect()
{
    group_name=`echo $1 | tr '[a-z]' '[A-Z]'`

    ASMCMD=${gridhome}/bin/asmcmd
    retval=`su - ${griduser} -c "${ASMCMD} mount ${group_name}"`

    if [ "$?" != "0" ];then
        echo "failed to mount diskgroup ${group_name}"
        exit 1
    fi
    echo "${group_name}"
}

disconnect()
{
    group_name=`echo $1 | tr '[a-z]' '[A-Z]'`

    ASMCMD=${gridhome}/bin/asmcmd
    retval=`su - ${griduser} -c "${ASMCMD} umount ${group_name}"`
    if [ "$?" != "0" ];then
        echo "failed to mount diskgroup ${group_name}"
        exit 1
    fi
    echo "${group_name}"
}

delete()
{
    group_name=`echo $1 | tr '[a-z]' '[A-Z]'`
    sid=$2
    force=$3

    ASMCMD=${gridhome}/bin/asmcmd
    status=`su - ${griduser} -c "${ASMCMD} ls" | grep ${group_name}`
    if [ -z "$status" ];then
      su - ${griduser} -c "${ASMCMD} mount ${group_name}"
    fi

    su - ${griduser} -c "${ASMCMD} dropdg ${group_name}"

    Sqlplus=${gridhome}/bin/sqlplus
    sql="drop diskgroup ${group_name} including contents;"
    if [ "$force" = "true" ];then
        sql="drop diskgroup ${group_name} force including contents;"
    fi
    export ORACLE_SID=${sid}
    retval=`su - ${griduser} -c "${Sqlplus} -s -L / as sysasm| grep -vi connected" << EOF
set pages 0
set head off
set feed off
set timing off
set echo off
set verify off
WHENEVER SQLERROR EXIT SQL.SQLCODE
${sql};
exit;
EOF`
    echo "${group_name}"
}

checkDiskString()
{
    export ORACLE_SID=${sid}
    Sqlplus=${gridhome}/bin/sqlplus

    # 检测并修改 asm_diskstring
    query="select value from v\$parameter where name='asm_diskstring'"
    retval=`su - ${griduser} -c "${Sqlplus} -s -L / as sysasm | grep -vi connected" << EOF
set pages 0
set head off
set feed off
set verify off
set timing off
set termout on
set lin 320
WHENEVER SQLERROR EXIT SQL.SQLCODE
${query};
exit;
EOF`
    out=`echo "$retval" | grep "/dev/rhl\*"`
    if [ -z "$out" ];then
      disk_string="'/dev/rhl#'"
      if [ -z "$retval" ];then
        disk_string="'/dev/rhl#'"
      else
        loop=`echo "$retval" | tr ',' ' ' | tr '*' '#'`
        for i in ${loop};do
          if [ -n "$i" ];then
            disk_string="${disk_string},'$i'"
          fi
        done
      fi
      disk_string=`echo ${disk_string} | tr '#' '*'`
	    retval=`su - ${griduser} -c "${Sqlplus} -s -L / as sysasm | grep -vi connected" << EOF
set pages 0
set head off
set feed off
set verify off
set timing off
set termout on
alter system set asm_diskstring=${disk_string};
exit;
EOF`
    fi
}

case ${op} in
create)
    dg=$2
    sid=$3
    disks=$4

    validate_name ${dg}
    validate_sid ${sid}
    validate_disks ${disks}

    create ${dg} ${sid} ${disks}
    ;;

delete)
    dg=$2
    sid=$3
    force=$4

    validate_name ${dg}
    validate_sid ${sid}
    validate_force ${force}

    delete ${dg} ${sid} ${force}
    ;;

rename)
    dg=$2
    sid=$3
    disks=$4
    newName=$5

    validate_name ${dg}
    validate_sid ${sid}
    validate_disks ${disks}
    validate_name ${newName}

    rename ${dg} ${sid} ${disks} ${newName}
    ;;

connect)
    dg=$2

    validate_name ${dg}

    connect ${dg}
    ;;

disconnect)

    dg=$2

    validate_name ${dg}

    disconnect ${dg}
    ;;

checkDiskString)
  checkDiskString
  ;;
*)
    echo_help
    exit 1
esac